package org.example.leetcpde;

import org.junit.jupiter.api.Test;

import java.util.BitSet;

public class lengthOfLongestSubstring {

    @Test
    void lengthOfLongestSubstring() {
        String aa = "aabaab!bb";
        System.out.println(lengthOfLongestSubstring(aa));
    }

    public int lengthOfLongestSubstring(String s) {
        if (s == null) {
            return 0;
        }

        if (s.length() == 1) {
            return 1;
        }

        byte[] bytes = s.getBytes();
        BitSet bitSet = new BitSet(bytes.length);
        int max = 0;
        for (int i = 0, j = 0; j < bytes.length; j++) {
            byte aByte = bytes[j];
            for (; bitSet.get(aByte); i++) {
                bitSet.clear(bytes[i]);
            }

            bitSet.set(aByte);
            max = Math.max(max, j - i +1);
        }

        return max;
    }
}
